﻿CREATE PROCEDURE [acms].[ControlType_Put]
	@Id int,
	@SiteId int,
	@Name varchar(50),
	@AdminControlType varchar(255),
	@SearchXPath varchar(100),
	@Custom int,
	@ConcurencyId rowversion,
	@IdOut int output,
	@ConcurencyIdOut rowversion output
	
AS
	if exists (select null from [ControlType_View] where Id = @Id) begin
		update [ControlTypes]
		set
			SiteId = @SiteId,
			Name = @Name,
			AdminControlType = @AdminControlType,
			SearchXPath = @SearchXPath,
			Custom = @Custom
		where
			Id = @Id
			and ConcurencyId = @ConcurencyId
			
		if @@Rowcount = 0
			return -100
		else
			set @IdOut = @Id
	end
	else if (@Id is null or @Id <= 0) begin
		insert into [ControlTypes]
			(SiteId, Name, AdminControlType, SearchXPath, Custom)
		values
			(@SiteId, @Name, @AdminControlType, @SearchXPath, @Custom)
		set @IdOut = @@Identity
	end
	else
		return -200
	
	select @ConcurencyIdOut = ConcurencyId From [ControlTypes] where Id = @Id
RETURN 0;